package com.contest.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.contest.dao.UserInfoDao;
import com.contest.entity.LimitSetup;
import com.contest.entity.UserInfo;
import com.contest.service.BettingRecordService;
import com.contest.service.LimitSetupService;
import com.contest.service.UserInfoService;
import com.contest.util.IdWorker;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * ${table.comment}  服务实现类
 * </p>
 *
 * @author yfl
 * @since ${date}
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoDao, UserInfo> implements UserInfoService {

    @Autowired
    IdWorker idWorker;
    @Autowired
    LimitSetupService limitSetupService;
    @Autowired
    BettingRecordService bettingRecordService;

    /**
     * 用户注册
     *
     * @param userInfo
     * @return
     */
    @Override
    @Transactional
    public boolean reg(UserInfo userInfo) {
        userInfo.setFdBettingTotal(BigDecimal.ZERO);
        userInfo.setFdRechargeTotal(BigDecimal.ZERO);
        userInfo.setFdOver(BigDecimal.ZERO);
        userInfo.setFdMyCode(UUID.randomUUID().toString().split("-")[4].toUpperCase());
        userInfo.setFdRoleId(2 + "");
        userInfo.setFdRoleName("普通用户");
        return this.insert(userInfo);
    }

    /**
     * 获取超额赔率
     */
    public float getLimitOdds(String userId) {
        UserInfo userInfo = this.selectById(userId);
        float limitScale = 0.0f;
        if (userInfo != null) {
            float todayAmount = bettingRecordService.getTodayByUserId(userId);
            return getLessScale(todayAmount);
        } else {
            return 0.0f;
        }
    }

    /**
     * 查看用户详情
     *
     * @param userId
     */
    @Override
    public UserInfo getUserDetailByUserId(String userId) {
        UserInfo userInfo = this.selectById(userId);
        float today = bettingRecordService.getTodayByUserId(userId);
        float mon = bettingRecordService.getMonByUserId(userId);
        float todayWin = bettingRecordService.getTodayWinByUserId(userId);
        float todayLose = bettingRecordService.getTodayLoseByUserId(userId);
        float monWin = bettingRecordService.getMonWinByUserId(userId);
        float monLose = bettingRecordService.getMonLoseByUserId(userId);
        userInfo.setToday(BigDecimal.valueOf(today));
        userInfo.setMon(BigDecimal.valueOf(mon));
        userInfo.setTodayLose(BigDecimal.valueOf(todayWin - todayLose));
        userInfo.setMonLose(BigDecimal.valueOf(monWin - monLose));
        return userInfo;
    }

    /**
     * 检查用户余额是否不足
     *
     * @param userId
     * @param amount
     */
    @Override
    public boolean checkUserOver(String userId, float amount) {
        UserInfo userInfo = this.selectById(userId);
        if (userInfo != null) {
            float over = userInfo.getFdOver().floatValue();
            return over >= amount;
        } else {
            return false;
        }
    }

    private float getLessScale(float todayAmount) {
        EntityWrapper<LimitSetup> limit = new EntityWrapper<LimitSetup>();
        limit.addFilter("fd_is_enable={0}", 1);
        List<LimitSetup> limitSetups = limitSetupService.selectList(limit);
        if (limitSetups.size() != 0) {
            LimitSetup setup = limitSetups.get(0);
            float limitScale = setup.getFdOdds().floatValue();
            float limitAmount = setup.getFdTodayLimit().floatValue();
            if(todayAmount >= limitAmount) {
                int temp = (int) (limitAmount / todayAmount);
                return temp * limitScale;
            }
            return 0;
        } else {
            return 0;
        }
    }
}
